package com.doit.excersise.controller;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doit.excersise.entity.TbExam;
import com.doit.excersise.entity.vo.Exam;
import com.doit.excersise.entity.vo.ExamType;
import com.doit.excersise.service.ITbExamService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qingfeng
 * @since 2018-11-23
 */
@Controller
@RequestMapping("/excersise/tb-exam")
public class TbExamController {
	@Autowired
	private ITbExamService examService;
	
	@RequestMapping("/getAll")
    @ResponseBody
	public Exam findAll(){
		Exam exam = new Exam();
		exam.setTitle("多易测试题1");
		
		List<ExamType> types = new ArrayList<>();
		ExamType t = new ExamType();
		QueryWrapper<TbExam> wrapper = new QueryWrapper<>();
		wrapper.eq("type", 1);
		wrapper.last("limit 10");
		List<TbExam> list = examService.list(wrapper);
		System.out.println(list);
		list.stream().map(e->{if(e.getOptions()!=null)e.setOptionsArr(e.getOptions().split(";"));return e;}).collect(Collectors.toList());
		t.setTitle("单选题");
		t.setValues(list);
		types.add(t);
		//多选
		wrapper = new QueryWrapper<>();
		wrapper.eq("type", 2);
		wrapper.last("limit 10");
		ExamType t2 = new ExamType();
		list = examService.list(wrapper);
		list.stream().map(e->{if(e.getOptions()!=null)e.setOptionsArr(e.getOptions().split(";"));return e;}).collect(Collectors.toList());
		t2.setTitle("多选题");
		t2.setValues(list);
		types.add(t2);
		
		//简答题
		wrapper = new QueryWrapper<>();
		wrapper.eq("type", 4);
		wrapper.last("limit 10");
		ExamType t3 = new ExamType();
		list = examService.list(wrapper);
		list.stream().map(e->{if(e.getOptions()!=null)e.setOptionsArr(e.getOptions().split(";"));return e;}).collect(Collectors.toList());
		t3.setTitle("简答题");
		t3.setValues(list);
		types.add(t3);
		
		exam.setExam(types);
		return exam;
	}
	
	@RequestMapping("/save")
	public String testInsert() {
		for(int i=0;i<55;i++) {
			TbExam s = new TbExam();
			s.setAnswer("封装继承多态抽象");
			s.setAnylize("围绕封装继承多态来回答");
			s.setContent("请简述面向对象的三大特征");
			s.setCoureseId(1);
			s.setCreateTime(LocalDateTime.now());
			s.setRemark("略");
			s.setOptions(null);
			s.setScore(10.0);
			s.setTeacher("Z某");
			s.setType(4);
			
			examService.save(s);
		}
		return "success";
	}
	
	//分页测试：
	@RequestMapping("findPage")
	@ResponseBody
	 public IPage<TbExam> findPage(@RequestBody Page<TbExam> p){
		/*System.out.println(page);
		System.out.println(pageSize);*/
		//Page<TbExam> p = new Page<TbExam>(page,pageSize);
		 IPage<TbExam> data = examService.page(p, null);
		 return data;
	 }
	
}

